package views;

import android.content.Context;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import Utils.DateUtil;
import app.MyApplication;
import greendao.UnitInformationDao;
import greendao.entity.UnitInformation;

/**
 * Changed by Jianqi on 2017/11/15.
 */

public class SearchBarView extends LinearLayout {
    ImageView search;
    //计划号
    EditText jhh;
    //施工日期
    TextView sgrq;
    //相关部门
    AutoCompleteTextView xgbm;
    //高级搜索layout
    LinearLayout Search_layout;
    //高级搜索layout是否显示
    boolean IsVisiable = false;
    //ImageView
    ImageView mImageView;
    List<UnitInformation> UnitList;
    OnClickListener onClickListener;
    Date date;

    public SearchBarView(Context context, OnClickListener onClickListener) {
        super(context);
        this.onClickListener = onClickListener;
        this.addView(inflate(getContext(), R.layout.searchbarview, null));
        InitView();
        InitDate();
        InitListen();
    }

    public void InitView() {
        jhh = (EditText) findViewById(R.id.edit);
        search = (ImageView) findViewById(R.id.imageview);
        sgrq = (TextView) findViewById(R.id.sgrq);
        xgbm = (AutoCompleteTextView) findViewById(R.id.xgbm);
        Search_layout = (LinearLayout) findViewById(R.id.Search_layout);
        mImageView = (ImageView) findViewById(R.id.mImageView);

    }

    public void InitDate() {
        date=new Date(System.currentTimeMillis());
        //给AutoCompleteTextView 设置数据源
        UnitList = MyApplication.getDaoSession().getUnitInformationDao().loadAll();
        xgbm.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, UnitList));
        //设置当前日期
            sgrq.setText(DateUtil.getYearAndMonthAndDay(date));
    }

    public void InitListen() {
        //输入框监听
        sgrq.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickerView.getInstance(getContext(), new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SearchBarView.this.date=date;
                        upDatedTitle();
                    }
                }).show();
            }
        });

        //搜索按钮监听
        search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onClick(v);
            }
        });
    }

    //更新标题栏日期并刷新列表数据
    public void upDatedTitle() {
        sgrq.setText(DateUtil.getYearAndMonthAndDay(date));
    }

    public String getJhh() {
        return jhh.getText().toString();
    }

    public String getSgrq() {
        return sgrq.getText().toString();
    }

    public String getXgbmId() {
        List<UnitInformation> Mylist = MyApplication.getDaoSession().getUnitInformationDao().queryBuilder()
                .where(UnitInformationDao.Properties.LjyyxDwxxName.eq(xgbm))
                .list();
        if (Mylist.size() > 0) {
            return Mylist.get(0).toString();
        }
        return null;
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

}
